<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">签到管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>用户管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            <!--            添加理由：由于具有查看所有人的权限所以添加可以选择的签到-->
            select选择框：
            <select id="sign-select">
                <option value="user_id" selected="selected">-个人签到-</option>
                <option value="all">-全部签到-</option>
                <!--                <option value="username">账号</option>-->
                <!--                <option value="nick_name">用户名</option>-->
                <!--                <option value="phone">手机号</option>-->
                <!--                <option value="email">邮箱</option>-->
            </select>&emsp;
            <!--            根据签到的状态选择搜索的对象-->
            签到状态：
            <select id="istate-select">
                <option value="" selected="selected">-未选择-</option>
                <option value="0">-未签到-</option>
                <option value="1">-已签到-</option>
                <option value="2">-迟到-</option>
            </select>
            <!--            根据签退的状态选择搜索的对象-->
            签到退状态：
            <select id="ostate-select">
                <option value="" selected="selected">-未选择-</option>
                <option value="0">-未签退-</option>
                <option value="1">-已签退-</option>
                <option value="2">-早退-</option>
            </select>&emsp;
            <!--            根据日期时间段查询签到表：如迟到的10分钟之内；如今日签到记录；昨日签到记录-->
            <!--            根据日期查询签到表：当未选择日期时：默认为今日；如今日至XX为今日 XX至今日为今日-->
            日期：
            <input id="timeStart" class="layui-input search-input" type="date" placeholder="输入：YYYY:MM:DD"/>至
            <input id="timeEnd" class="layui-input search-input" type="date" placeholder="输入：YYYY:MM:DD"/>
            <!--            根据时间查询签到表中时间段：如查询9:00-9:30的签到记录：包括签到和签退-->
            时间：
            <input id="dateStart" class="layui-input search-input" type="time" placeholder="输入：hh:mm:nn"/>至
            <input id="dateEnd" class="layui-input search-input" type="time" placeholder="输入：hh:mm:nn"/>
            <!--            根据昵称查询签到表中的数据-->
            用户昵称：
            <input id="nickName" class="layui-input search-input" type="text" placeholder="输入用户昵称"/>&emsp;
            <button id="sign-btn-sreach" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>搜索</button>
            <button id="sign-btn-signIn" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>签到</button>
            <button id="sign-btn-signOut" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>签退</button>
            <button id="sign-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>
            <button id="attendance-btn-time" class="layui-btn icon-btn"><i class="layui-icon">&#xe604;</i>考勤时间管理</button>
        </div>

        <table class="layui-table" id="sign-table" lay-filter="sign-table"></table>
    </div>
</div>
<script type="text/html" id="sign-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="delete">删除</a>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'index', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var index = layui.index;
        var admin = layui.admin;
        form.render();

        // 添加select绑定权限
        if(!admin.hasPerm('sign:post/sign/userSelect')) {
            $('#sign-select').append("<option value=\"\">-全部签到-</option>");
        }
        // 考勤时间管理按钮绑定权限
        // if(!admin.hasPerm('sign:post/sign/findSignInAndSignOutTime')) {
        //     $('#attendance-btn-time').hide();
        // }
        var login_user = '';
        // 获取当前用户信息
        index.getUser(function (user) {
            login_user = user;
            console.log(login_user)
        });

        // 参数按钮点击事件
        $('#attendance-btn-time').click(function () {
            console.log(1);
            let attr="sign";
            let data={
                attr:attr,
                access_token: config.getToken().access_token
            }
            console.log(data);
            admin.req("api-sign/sign/getParamByAttr", JSON.stringify(data), function (res) {
                // console.log(res);
                console.log(res);
                showTimeModel(res);
            }, "POST");

        });
        //
        var showTimeModel = function (res) {
            admin.putTempData('t_signParam', res);
            var title =  '签到参数修改' ;
            admin.popupCenter({
                title: title,
                path: 'pages/attendance/signParamUpdate.html',
                finish: function () {
                    table.reload('sign-table', {});
                }
            });
        }

        var userSelect=$("#sign-select").val();
        var userId;
        if(userSelect!="all"){
            userId=login_user.id;
        }else{
            userId="";
        }
        // 渲染表格
        table.render({
            elem: '#sign-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-sign/sign/signManage',
            method: 'POST',
            where: {
                access_token: config.getToken().access_token,
                userId: userId
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                //修改理由：由于不用展示userId字段故注释
                // {field: 'userId', sort: true, title: '用户编号'},
                //添加理由：在签到栏一项需要展示用户昵称字段；在model层增加nickName字段
                {field: 'nickName', sort: true, title: '用户昵称'},
                {field: 'signin', sort: true, title: '签到时间'},
                {field: 'sigout', sort: true, title: '签退时间'},
                {
                    sort: true, title: '签到状态', templet: function (d) {
                        if (d.istate == 1) {
                            return "已签到";
                        } else if (d.istate == 2) {
                            return "已迟到"
                        } else {
                            return "未签到"
                        }
                    }
                },
                {
                    sort: true, title: '签退状态', templet: function (d) {
                        if (d.ostate == 1) {
                            return "已签退";
                        } else if (d.ostate == 2) {
                            return "早退"
                        } else {
                            return "未签退"
                        }
                    }
                },
                {field: 'time', sort: true, title: '签到日期'},
                {align: 'center', toolbar: '#sign-table-bar', title: '操作', width: 150}
            ]]
        });
        // 搜索按钮点击事件
        $('#sign-btn-sreach').click(function () {
            //查询用户昵称：nickName
            var nickName = $('#nickName').val();
        //     // //查询开始日期：timeStart
             var timeStart = $('#timeStart').val();
        //     // //查询结束日期：timeEnd
            var timeEnd=$('#timeEnd').val();
        //     // //查询开始时间：dateStart
             var dateStart=$('#dateStart').val();
        //     // //查询结束时间：dateEnd
             var dateEnd =$('#dateEnd').val();

            // //查询签到状态：istate
               var istate=$('#istate-select').val();
        //     // //查询签退状态：ostate

               var ostate=$('#ostate-select').val();
            var userSelect=$("#sign-select").val();
            var userId;
            if(userSelect!="all"){
                userId=login_user.id;
            }else{
                userId="";
            }
            let data={
                nickName:nickName,
                userId:userId,
                timeStart:timeStart,
                timeEnd:timeStart,
                dateStart:dateStart,
                dateEnd:dateEnd,
                istate:istate,
                ostate:ostate
            }
            console.log(11111)
            console.log(data,11111);
        //     // table.reload('sign-table', {where: {nickName: nickName,timeStart: timeStart,timeEnd:timeEnd,dateStart:dateStart,dateEnd:dateEnd,istate:istate,ostate:ostate}});
        //     table.reload('sign-table',{where:{nickName:nickName,userId: login_user.id}});
            table.render({
                elem: '#sign-table',
                // url: config.base_server + 'user_query.json',
                url: config.base_server + 'api-sign/sign/signManage',
                method: 'POST',
                where: {
                    access_token: config.getToken().access_token,
                    userId: userId,
                    timeStart:timeStart,
                    timeEnd:timeEnd,
                    dateStart:dateStart,
                    dateEnd:dateEnd,
                    istate:istate,
                    ostate:ostate,
                    nickName:nickName
                },
                page: true,
                cols: [[
                    {type: 'numbers'},
                    //修改理由：由于不用展示userId字段故注释
                    // {field: 'userId', sort: true, title: '用户编号'},
                    //添加理由：在签到栏一项需要展示用户昵称字段；在model层增加nickName字段
                    {field: 'nickName', sort: true, title: '用户昵称'},
                    {field: 'signin', sort: true, title: '签到时间'},
                    {field: 'sigout', sort: true, title: '签退时间'},
                    {
                        sort: true, title: '签到状态', templet: function (d) {
                            if (d.istate == 1) {
                                return "已签到";
                            } else if (d.istate == 2) {
                                return "已迟到"
                            } else {
                                return "未签到"
                            }
                        }
                    },
                    {
                        sort: true, title: '签退状态', templet: function (d) {
                            if (d.ostate == 1) {
                                return "已签退";
                            } else if (d.ostate == 2) {
                                return "早退"
                            } else {
                                return "未签退"
                            }
                        }
                    },
                    {field: 'time', sort: true, title: '签到日期'},
                    {align: 'center', toolbar: '#sign-table-bar', title: '操作', width: 150}
                ]]
            });
        });

        // 工具条点击事件
        table.on('tool(sign-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') {// 修改
                showEditModel(data);
            }else if(layEvent==="delete"){//删除
                deleteSign(data);
            }
        });

        //删除当前数据
        var deleteSign=function (data) {
            layer.confirm('是否确定删除？', function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('api-sign/sign/signDelete',JSON.stringify(data), function (data) {
                    layer.closeAll('loading');
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'POST');
                table.reload('sign-table', {});
            });
        }

        //显示表单弹窗
        var showEditModel = function (data) {
            //修改sex值
            if (data) {
                data.state = data.state + '';
            }
            admin.putTempData('t_sign', data);
            var title =  '签到修改' ;
            admin.popupCenter({
                title: title,
                path: 'pages/attendance/signUpdate.html',
                area:['1200px','800px'],
                finish: function () {
                    table.reload('sign-table', {});
                }
            });
        };

        // 导出
        $('#sign-btn-export').click(function () {
            // let data={
            //     access_token: config.getToken().access_token,
            //     userId: login_user.id
            // }
            var url = config.base_server + 'api-sign/sign/exportSign?access_token='+config.getToken().access_token+'&userId='+login_user.id;
            var xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);
            xhr.responseType = "blob";
            xhr.setRequestHeader("client_type", "DESKTOP_WEB");
            xhr.onload = function () {
                if (this.status == 200) {
                    var blob = this.response;
                    var objecturl = URL.createObjectURL(blob);
                    window.location.href = objecturl;
                }
            }
            xhr.send();
        });



        // //select select的更改触发
        // $("#sign-select").change(function () {
        //     console.log($(this).val());
        //     var options=$(this).val();
        //     table.reload('sign-table',{where:{options:options}});
        // })
    });
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        let admin = layui.admin;
        let index = layui.index;
        form.render();

        $('#sign-btn-signIn').click(function () {

            var login_user;
            // 获取当前用户信息
            index.getUser(function (user) {
                login_user = user;
                console.log(login_user)
            });
            let data = {
                userId: login_user.id
            }
            admin.req("api-sign/sign/signIn", JSON.stringify(data), function (res) {
                // layer.load(2);
                console.log(res);
                if (res.data == 1) {
                    layer.open({
                        content: '签到成功！'
                    });
                } else if (res.data == 2) {
                    layer.open({
                        content: '已签到,不能重复签到！'
                    });
                } else if (res.data == 3) {
                    layer.open({
                        content: '已签退，无法签到'
                    });
                } else if (res.data == 4) {
                    layer.open({
                        content: '迟到'
                    });
                } else if (res.data == -1) {
                    layer.open({
                        content: '系统错误'
                    });
                }

            }, "POST");
            table.reload('sign-table', {});
        })

    })
</script>
<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        let admin = layui.admin;
        let index = layui.index;
        form.render();

        $('#sign-btn-signOut').click(function () {

            var login_user;
            // 获取当前用户信息
            index.getUser(function (user) {
                login_user = user;
                console.log(login_user)
            });
            let data = {
                userId: login_user.id
            }
            admin.req("api-sign/sign/signOut", JSON.stringify(data), function (res) {
                console.log(res);
                if (res.data == 1) {
                    layer.open({
                        content: '签退成功！'
                    });
                } else if (res.data == 2) {
                    layer.open({
                        content: '已签退,不能重复签退'
                    });
                } else if (res.data == 3) {
                    layer.open({
                        content: '早退！'
                    });
                } else if (res.data == -1) {
                    layer.open({
                        content: '系统错误！'

                    });
                } else if (res.data == 4) {
                    layer.confirm("tip",{content:"先提交日报才能签退！"},function () {
                        layer.closeAll();
                        showEditModel();
                    })
                }
            }, "POST")

            table.reload('sign-table', {})
        })
        var showEditModel = function (data) {
            //修改sex值
            if (data) {
                data.state = data.state + '';
            }
            admin.putTempData('t_daily', data);
            var title = data ? '日报修改' : '日报提交';
            admin.popupCenter({
                title: title,
                path: 'pages/attendance/dailySubmit.html',
                area: ['1200px', '800px'],
                finish: function () {
                    table.reload('daily-table', {});
                }
            });
        };
    })
</script>

<!--<script>-->
<!--    layui.use('laydate', function() {-->
<!--        var laydate = layui.laydate;-->
<!--        laydate.render({-->
<!--            elem: '#timeStart'-->
<!--        });-->
<!--        laydate.render({-->
<!--            elem: '#timeEnd'-->
<!--        });-->
<!--        laydate.render({-->
<!--            elem: '#dateStart'-->
<!--            , type: 'time'-->
<!--        });-->
<!--        laydate.render({-->
<!--            elem: '#dateEnd'-->
<!--            , type: 'time'-->
<!--        });-->

<!--    })-->

<!--</script>-->
